import {
    getamapHandle
} from './init'
import upperCamelCase from 'uppercamelcase';
import constants from './constants'
import VueAMap from '../'
import {
    commonConvertMap
} from './convert-helper'
import eventHelper from '../utils/event-helper'

export default {

    mounted() {
        this.$map = this.$map || this.$parent.$map
        if (this.$amap) {
            this.register();
        } else {
            this.$on(constants.AMAP_READY_EVENT, map => {
                this.$amap = map;
                console.log('AMAP_READY_EVENT');
                this.register();
            });
        }
    },
    methods: {
        register() {
            console.log('register');
            this._initComponents && this._initComponents(this.convertProps())
            this.registerEvents()
        },
        registerEvents() {
            if (!this.$options.propsData) return;
            if (this.$options.propsData.events) {
                for (let eventName in this.events) {
                    eventHelper.addListener(this.$amapComponent, eventName, this.events[eventName]);
                }
            }
            if (this.events && this.events.init) this.events.init(this.$amapComponent, this.$amap);
        },
        convertProps() {
            const props = {};
            if (this.$amap) props.map = this.$amap;
            const {
                $options: {
                    propsData = {}
                },
                propsRedirect
            } = this;

            console.log('props option', propsData);
            console.log('propsRedirect', propsRedirect);
            return Object.keys(propsData).reduce((res, _key) => {
                let key = _key;
                let propsValue = this.convertSignalProp(key, propsData[key]);
                if (propsValue === undefined) return res;
                if (propsRedirect && propsRedirect[_key]) key = propsRedirect[key];
                props[key] = propsValue;
                return res;
            }, props);
        },
        convertSignalProp(key, sourceData) {
            let converter = '';
            let type = '';

            if (this.amapTagName) {
                try {
                    const name = upperCamelCase(this.amapTagName).replace(/^El/, '');
                    const componentConfig = VueAMap[name] || '';

                    type = componentConfig.props[key].$type;
                    converter = commonConvertMap[type];
                } catch (e) {}
            }

            if (type && converter) {
                return converter(sourceData);
            } else if (this.converters && this.converters[key]) {
                console.log('converters key', key);
                return this.converters[key].call(this, sourceData);
            } else {
                const convertFn = commonConvertMap[key];
                console.log('convertFn', convertFn);
                if (convertFn) return convertFn(sourceData);
                return sourceData;
            }
        },
    }
}
